package com.wangtao.firstapp.fragments.homefragments;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.google.gson.Gson;
import com.squareup.picasso.Picasso;
import com.wangtao.firstapp.R;
import com.wangtao.firstapp.activity.ModifyNameSignActivity;
import com.wangtao.firstapp.activity.MyAttentionActivity;
import com.wangtao.firstapp.activity.MyCollectionActivity;
import com.wangtao.firstapp.activity.MyFansActivity;
import com.wangtao.firstapp.activity.MyOrderActivity;
import com.wangtao.firstapp.activity.MyOriginalActivity;
import com.wangtao.firstapp.activity.PhotoPickActivity;
import com.wangtao.firstapp.activity.SystemSetUpActivity;
import com.wangtao.firstapp.modles.MyAttentionData;
import com.wangtao.firstapp.utils.HttpUtils;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 个人界面
 * Created by lanouhn on 16/8/24.
 */
public class PersonFragment extends Fragment {
    private CircleImageView cir_header_person;// 用户头像
    private TextView tv_username_person;// 用户昵称
    private TextView tv_sign_person;// 用户个性签名
    private TextView tv_likecount_person;// 用户喜欢数
    private TextView tv_followcount_person;// 用户关注数
    private TextView tv_fanscount_person;// 用户粉丝数
    private RelativeLayout mycollection_person;// 我的收藏
    private RelativeLayout myoriginal_person;// 我的原创
    private RelativeLayout myorder_person;// 我的订单
    private RelativeLayout exchangepresent;// 礼品兑换
    private RelativeLayout Invitecode;// 邀请码兑换
    private RelativeLayout systemsetup;// 设置
    private PopupWindow popupwindow;// 点击头像弹出的PopupWindow
    private LinearLayout changeheader_popupwindow;// PopupWindow中设置头像
    private LinearLayout changenamesign_popupwindow;// PopupWindow中设置昵称和个性签名
    private LinearLayout cancel_popupwindow;// PopupWindow中取消
    private LinearLayout myattention;// 我的关注
    private LinearLayout myfans;// 我的粉丝
    private MyAttentionData myAttentionData;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.person, null);
        cir_header_person = (CircleImageView) view.findViewById(R.id.cir_header_person);// 用户头像
        tv_username_person = (TextView) view.findViewById(R.id.tv_username_person);// 用户昵称
        tv_sign_person = (TextView) view.findViewById(R.id.tv_sign_person);// 用户个性签名
        tv_likecount_person = (TextView) view.findViewById(R.id.tv_likecount_person);// 用户喜欢数
        tv_followcount_person = (TextView) view.findViewById(R.id.tv_followcount_person);// 用户关注数
        tv_fanscount_person = (TextView) view.findViewById(R.id.tv_fanscount_person);// 用户粉丝数
        mycollection_person = (RelativeLayout) view.findViewById(R.id.mycollection_person);// 我的收藏
        myoriginal_person = (RelativeLayout) view.findViewById(R.id.myoriginal_person);// 我的原创
        myorder_person = (RelativeLayout) view.findViewById(R.id.myorder_person);// 我的订单
        exchangepresent = (RelativeLayout) view.findViewById(R.id.exchangepresent);// 礼品兑换
        Invitecode = (RelativeLayout) view.findViewById(R.id.Invitecode);// 邀请码兑换
        systemsetup = (RelativeLayout) view.findViewById(R.id.systemsetup);// 设置
        myattention = (LinearLayout) view.findViewById(R.id.myattention);// 我的关注
        myfans = (LinearLayout) view.findViewById(R.id.myfans);// 我的粉丝
        // initData();
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // 头像点击事件 弹出PopupWindow
        cir_header_person.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // show();
                showDialog();
            }
        });
        // 我的收藏
        mycollection_person.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent_mycollection = new Intent(getActivity(), MyCollectionActivity.class);
                startActivity(intent_mycollection);
            }
        });
        // 我的原创
        myoriginal_person.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent_myoriginal = new Intent(getActivity(), MyOriginalActivity.class);
                startActivity(intent_myoriginal);
            }
        });
        // 我的订单
        myorder_person.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent_myorder = new Intent(getActivity(), MyOrderActivity.class);
                startActivity(intent_myorder);
            }
        });


        // 系统设置
        systemsetup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent_systemsetup = new Intent(getActivity(), SystemSetUpActivity.class);
                startActivity(intent_systemsetup);
            }
        });

        // 我的关注
        myattention.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent_myattention = new Intent(getActivity(), MyAttentionActivity.class);
                // startActivity(intent_myattention);
                startActivityForResult(intent_myattention, 104);
            }
        });
        // 我的粉丝
        myfans.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent_myfans = new Intent(getActivity(), MyFansActivity.class);
                startActivity(intent_myfans);
            }
        });


    }

    private void showDialog() {
        AlertDialog.Builder ab = new AlertDialog.Builder(getActivity());
        ab.setItems(new String[]{"修改头像", "修改昵称和个性签名", "取消"}, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which) {
                    case 0:
                        Intent intent_header = new Intent(getActivity(), PhotoPickActivity.class);
                        startActivityForResult(intent_header, 103);
                        break;
                    case 1:
                        Intent intent_name = new Intent(getActivity(), ModifyNameSignActivity.class);
                        startActivityForResult(intent_name, 101);
                        break;
                    case 2:
                        dialog.dismiss();
                        break;
                }
            }
        });
        ab.show();
    }

//    private void show() {
//        View view = LayoutInflater.from(getActivity()).inflate(R.layout.header_popupwindow_person, null);
//        popupwindow = new PopupWindow(view, 400, 213, true);
//        popupwindow.setContentView(view);
//        popupwindow.setFocusable(false);
//        changeheader_popupwindow = (LinearLayout) view.findViewById(R.id.changeheader_popupwindow);// 修改头像
//        changenamesign_popupwindow = (LinearLayout) view.findViewById(R.id.changenamesign_popupwindow);// 修改昵称和个性签名
//        cancel_popupwindow = (LinearLayout) view.findViewById(R.id.cancel_popupwindow);// 取消
//        // 设置头像
//        changeheader_popupwindow.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent_header = new Intent(getActivity(), PhotoPickActivity.class);
//                startActivity(intent_header);
//                popupwindow.dismiss();
//            }
//        });
//        // 设置昵称和个性签名
//        changenamesign_popupwindow.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Intent intent_name = new Intent(getActivity(), ModifyNameSignActivity.class);
//                startActivityForResult(intent_name, 101);
//                popupwindow.dismiss();
//            }
//        });
//        // 取消
//        cancel_popupwindow.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                popupwindow.dismiss();
//            }
//        });
//        popupwindow.setBackgroundDrawable(new BitmapDrawable());
//        popupwindow.setOutsideTouchable(true);
//        View rootview = LayoutInflater.from(getActivity()).inflate(R.layout.person, null);
//        popupwindow.showAtLocation(rootview, Gravity.CENTER, 0, 0);
//    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 101 && resultCode == 102) {
            Bundle bundle = data.getExtras();
            String name = bundle.getString("name");
            String sign = bundle.getString("sign");
            tv_username_person.setText(name);
            tv_sign_person.setText(sign);
        }
        if (requestCode == 103 && resultCode == Activity.RESULT_OK) {
            String imagepath = data.getStringArrayListExtra("SELECTED_PHOTOS").get(0);
            //  data/media/0/Download/
            //  String image ="/storage/emulated/0/Download/d4628535e5dde711e09861aaa5efce1b9d166127.png";

            Picasso.with(getActivity()).load(imagepath).config(Bitmap.Config.RGB_565).into(cir_header_person);
        }
    }

    private void initData() {

        String url = "http://open4.bantangapp.com/users/fllow/listAttention";
        String param = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2559956&oauth_token=6beffa8c92276d19d3c23acb596024e6&track_deviceid=866962021116243&track_device_info=m1+note&channel_name=sougou&app_installtime=1471923362&app_versions=5.8.8&os_versions=5.1&screensize=1080&v=19&page=0&pagesize=10&visit_user_id=2559956";
        try {
            HttpUtils.doPostAsyn(url, param, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson = new Gson();
                    myAttentionData = gson.fromJson(result, MyAttentionData.class);
                    tv_followcount_person.setText("" + myAttentionData.getData().getList().size());
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
